<?php

namespace Biz\model\taobao;

use Biz\tools\TbkUnion;
use Doraemon\pockets\datebase\BaseDbModel;

class TaoBaoUserAuth extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'taobao_user_auth';

    const STATE_NORMAL = 1;//正常
    const STATE_DEL = 9;//删除

    private static function getAppKey($scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = TbkUnion::getAppKey();//目前使用淘宝联盟，appkey可以用固定的
        return $appKey;
    }

    /**
     * 根据uid获取relation_id
     * @param $uid
     * @param string $scene
     * @return mixed|string
     */
    public static function getRelationIdByUid($uid, $scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = self::getAppKey($scene);
        $info = self::getByWhere(['uid' => $uid, 'app_key' => $appKey, 'relation_state' => self::STATE_NORMAL], ['relation_id', 'app_key', 'uid']);
        return $info['relation_id'] ?? '';
    }


    /**
     * 根据relation_id获取uid
     * @param $relation_ids
     * @param string $scene
     * @return mixed|string
     */
    public static function getUidByRelationId($relation_id, $scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = self::getAppKey($scene);
        $info = self::getByWhere(['relation_id' => $relation_id, 'app_key' => $appKey, 'relation_state' => self::STATE_NORMAL], ['relation_id', 'app_key', 'uid']);
        return $info['uid'] ?? '';
    }

    /**
     * 根据relation_ids获取uids
     * @param $relation_ids
     * @param string $scene
     * @return mixed|string
     */
    public static function getUidsByRelationIds($relation_ids, $scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = self::getAppKey($scene);
        $infos = self::selectByWhere(['relation_id' => $relation_ids, 'app_key' => $appKey, 'relation_state' => self::STATE_NORMAL], ['relation_id', 'app_key', 'uid']);
        $infos = array_column($infos, null, 'relation_id');
        $result = [];
        foreach ($relation_ids as $relation_id) {
            if (isset($infos[$relation_id])) {
                $result[$relation_id] = $infos[$relation_id]['uid'];
            } else {
                $result[$relation_id] = '';
            }
        }
        return $result;
    }

    /**
     * 根据uids获取relation_id
     * @param $uid
     * @param string $scene
     * @return mixed|string
     */
    public static function getRelationIdsByUid(array $uids, $scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = self::getAppKey($scene);
        $infos = self::selectByWhere(['uid' => $uids, 'app_key' => $appKey, 'relation_state' => self::STATE_NORMAL], ['relation_id', 'app_key', 'uid']);
        $infos = array_column($infos, null, 'uid');
        $result = [];
        foreach ($uids as $uid) {
            if (isset($infos[$uid])) {
                $result[$uid] = $infos[$uid]['relation_id'];
            } else {
                $result[$uid] = '';
            }
        }
        return $result;
    }

    public static function addData($uid, $appKey, $data)
    {
        $info = self::getByWhere(['uid' => $uid, 'app_key' => $appKey], ['uid', 'app_key']);
        if ($info) {
            return self::updateData($data, ['uid' => $uid, 'app_key' => $appKey]);
        } else {
            return self::insertData($data);
        }
    }

    public static function getByWhere($where, $field = '*')
    {
        return self::get($field, $where);
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);

        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);

        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    /**
     * 解除用户uid和渠道关系id的关系
     * @param $uid
     * @param $relationId
     * @param string $scene
     * @return bool
     */
    public static function delUerRelationId($uid, $relationId, $scene = TbkUnion::OAUTH_SCENE_DESIGNER_COMMERCE_RID)
    {
        $appKey = self::getAppKey($scene);
        return self::updateData(['relation_state' => self::STATE_DEL], ['uid' => $uid, 'app_key' => $appKey, 'relation_id' => $relationId]);
    }

}